package com.kpiJavaTraining.bogdan.module2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class LearnController {
	
	private ArrayList<LearnQuestionsByTopic> learnQuestionsByTopic;
	private int countOfAsk=0;
	private final int  percentOfFirstQuestions;
	private final  int averageDifficulty;
	
	public LearnController() throws ClassNotFoundException, IOException, CloneNotSupportedException{
		ArrayList<LearnQuestion> learnQuestions;
		InitLearnQuestions init= new InitLearnQuestions();
		learnQuestions=init.initLearnQuestions();
		this.percentOfFirstQuestions =  (int)calculateTwentyPercent(learnQuestions.size()) ; 
		initLearnQuestionsByTopic(learnQuestions);
		int calculatedAverageDifficulty=determineAverageDifficulty();
		this.averageDifficulty=calculatedAverageDifficulty;
	}
	
	private float calculateTwentyPercent(int size){
		float localSize= (float) size;
		 return (localSize/(float)100)*(float)50;
	}
	
	private final int determineAverageDifficulty(){
		int averageDifficulty;
		int summuryAverageDifficulty=0;
		for(LearnQuestionsByTopic e: this.learnQuestionsByTopic){
			e.determineAvarageDifficulty();
			summuryAverageDifficulty+=e.getAverageDifficulty();
		}
		averageDifficulty=summuryAverageDifficulty/this.learnQuestionsByTopic.size();
		return averageDifficulty;
	}
	
	private void initLearnQuestionsByTopic(ArrayList<LearnQuestion> learnQuestions) {
		ArrayList<String> topics=initTopics(learnQuestions);
		this.learnQuestionsByTopic= new ArrayList<LearnQuestionsByTopic>(topics.size());
		for(String e:topics){
			LearnQuestionsByTopic temp= new LearnQuestionsByTopic();
			for(LearnQuestion i:learnQuestions){
				if(e.equals(i.getTopic())){
					temp.add(i);
				}
			}
			this.learnQuestionsByTopic.add(temp);
		}
	}
	//good
	private ArrayList<String> initTopics(ArrayList<LearnQuestion> learnQuestions){
		ArrayList<String> topics= new ArrayList<String>();
		for(LearnQuestion e :learnQuestions){
			if(!topics.contains(e.getTopic())){
				topics.add(e.getTopic());
			}
		}
		return topics;
	}
	
	public void learn(){		
		while(!flag()){
			askNextQuestion();
			countOfAsk++;
		}
		new Printer().println("Finish of learning");		
	}
	
	private boolean flag(){
		boolean  flagOfLearning=true;
		for(LearnQuestionsByTopic e:this.learnQuestionsByTopic){
			flagOfLearning=flagOfLearning&& e.getLearning();
		}
		return flagOfLearning;
	}
	
	
	
	private void askNextQuestion(){
		if(countOfAsk<this.percentOfFirstQuestions){
			askFirstRandomQuestion();
		}
		else askSecondQuestion();
	}
	
	private void askSecondQuestion(){
		LearnQuestionsByTopic  temp;
		temp =chooseTopicOfQuestion();
		temp.ask();
	}
	
	
	private void askFirstRandomQuestion(){
		Random generator= new Random();
		int numberOfTopic;
		int generatedForTopic;
		do{
			generatedForTopic= generator.nextInt();
			numberOfTopic= Math.abs(generatedForTopic%this.learnQuestionsByTopic.size());
		}
		while(!this.learnQuestionsByTopic.get(numberOfTopic).isAllQuestionAsking()
			&&this.averageDifficulty>this.learnQuestionsByTopic.get(numberOfTopic).getAverageDifficulty());
		this.learnQuestionsByTopic.get(numberOfTopic).askRandom();
	}
	
	
	private LearnQuestionsByTopic chooseTopicOfQuestion(){
		LearnQuestionsByTopic min=null;
		for(LearnQuestionsByTopic e:this.learnQuestionsByTopic){
			if(!e.getLearning()){
				min=e;
				break;
			}
		}
		
		for(int i=0;i<this.learnQuestionsByTopic.size(); i++){
			if(min.getLevelOfGrasp()>this.learnQuestionsByTopic.get(i).getLevelOfGrasp()
			   &&!this.learnQuestionsByTopic.get(i).getLearning()){
				min=this.learnQuestionsByTopic.get(i);
			}
		}
		return min;
	}
	
	
}
